package com.huike.web.controller.report;

import com.github.pagehelper.Page;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.contract.domain.TbContract;
import com.huike.report.domain.vo.*;
import com.huike.report.service.IReportService;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.report.service.ReportService;
import io.swagger.annotations.Api;
import com.huike.report.result.PageResult;
import com.huike.report.service.ReportService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.report.domain.vo.DeptStatisticsListVO;
import com.huike.report.domain.vo.LineChartVo;
import com.huike.report.dto.ContractStatisticsListDTO;
import com.huike.report.result.ReportSalesDeptStatisticsListData;
import com.huike.report.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * @Description 统计分析相关接口
 * @Date 2023-10-10
 */

@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "统计分析相关接口")
@RequiredArgsConstructor
public class ReportController extends BaseController {


    private final ReportService reportService;


    /**
     * 渠道统计
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @ApiOperation("渠道统计接口")
    @GetMapping("/chanelStatistics/{beginCreateTime}/{endCreateTime}")
    public AjaxResult chanelStatistics(@PathVariable String beginCreateTime,
                                       @PathVariable String endCreateTime){

        return AjaxResult.success(reportService.chanelStatistics(beginCreateTime,endCreateTime));
    }

    /**
     * 渠道统计-活动统计
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @ApiOperation("渠道统计-活动统计")
    @GetMapping("activityStatistics/{beginCreateTime}/{endCreateTime}")
    public AjaxResult activityStatistics(@PathVariable String beginCreateTime,
                                         @PathVariable String endCreateTime){
        return AjaxResult.success(reportService.activityStatistics(beginCreateTime,endCreateTime));
    }
    /**
     * 活动详情列表
     * @param activityPageBeanDto
     * @return
     */
    @ApiOperation("活动详情列表")
    @GetMapping("/activityStatisticsList")
    public PageResult activityStatisticsList(ActivityPageBeanDto activityPageBeanDto) {
        Page<ActivityStatisticsVo> p = reportService.getActivityStatisticsList(activityPageBeanDto);
        if (!ObjectUtils.isEmpty(p)) {
            return PageResult.success(p.getTotal(), p.getResult());
        }
        return PageResult.success(0,null);
    }


    /**
     * 处理客户统计新增客户数报表
     * @return
     */
    @ApiOperation("客户统计新增客户数报表接口")
    @GetMapping("/contractStatistics/{beginCreateTime}/{endCreateTime}")
    public LineChartVo contractStatistics(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime
                                         ,@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime){

        log.info("开始执行客户统计新增客户数报表接口：{}，{}",beginCreateTime,endCreateTime);

        LineChartVo lineChartVo = reportService.contractStatistics(beginCreateTime,endCreateTime);

        return lineChartVo;
    }






    /**
     * 处理销售统计归属部门明细列表
     * @return
     */
    @ApiOperation("销售统计归属部门明细列表")
    @GetMapping("/deptStatisticsList/{beginCreateTime}/{endCreateTime}")
    public DeptStatisticsListVO deptStatisticsList(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime
                                        , @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime
                                        , Integer pageNum , Integer pageSize){

        log.info("开始执行销售统计归属部门明细列表：{}，{}",beginCreateTime,endCreateTime,pageNum,pageSize);

        DeptStatisticsListVO deptStatisticsListVO = reportService.deptStatisticsList(beginCreateTime,endCreateTime,pageNum,pageSize);

        if (deptStatisticsListVO != null){
            AjaxResult success = AjaxResult.success();
            BeanUtils.copyProperties(success,deptStatisticsListVO);
        }else {
            AjaxResult success = AjaxResult.error();
            BeanUtils.copyProperties(success,deptStatisticsListVO);
        }
        return deptStatisticsListVO;
    }


    /**
     * 处理客户统计明细列表
     * @return
     */
    @ApiOperation("客户统计明细列表接口")
    @GetMapping("/contractStatisticsList")
    public ContractStatisticsListVO contractStatisticsList(ContractStatisticsListDTO contractStatisticsListDTO){

        log.info("正在执行客户统计明细列表接口：{}",contractStatisticsListDTO);

        ContractStatisticsListVO contractStatisticsListVO = reportService.contractStatisticsList(contractStatisticsListDTO);

        if (contractStatisticsListVO != null){
            AjaxResult success = AjaxResult.success();
            BeanUtils.copyProperties(success,contractStatisticsListVO);
        }else {
            AjaxResult success = AjaxResult.error();
            BeanUtils.copyProperties(success,contractStatisticsListVO);
        }


        return contractStatisticsListVO;


    }




    /**
     * 处理销售统计报表
     * @return
     */
    @ApiOperation("销售统计报表接口")
    @GetMapping("/salesStatistics/{beginCreateTime}/{endCreateTime}")
    public LineChartVo salesStatistics(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime
            ,@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime){

        log.info("开始执行销售统计报表接口：{}，{}",beginCreateTime,endCreateTime);

        LineChartVo lineChartVo = reportService.salesStatistics(beginCreateTime,endCreateTime);

        return lineChartVo;
    }




    /**
     * 销售统计归属渠道明细列表
     * @return
     */
    @ApiOperation("销售统计归属渠道明细列表接口")
    @GetMapping("/channelStatisticsList/{beginCreateTime}/{endCreateTime}")
    public ChannelStatisticsListVO channelStatisticsList(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime
            , @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime){

        log.info("开始执行销售统计归属渠道明细列表接口：{}，{}",beginCreateTime,endCreateTime);

        ChannelStatisticsListVO channelStatisticsList = reportService.channelStatisticsList(beginCreateTime,endCreateTime);

        if (channelStatisticsList != null){
            AjaxResult success = AjaxResult.success();
            BeanUtils.copyProperties(success,channelStatisticsList);
        }else {
            AjaxResult success = AjaxResult.error();
            BeanUtils.copyProperties(success,channelStatisticsList);
        }

        return channelStatisticsList;
    }






    /**
     * 处理销售统计归属人报表
     * @return
     */
    @ApiOperation("销售统计归属人报表")
    @GetMapping("/ownerShipStatisticsList/{beginCreateTime}/{endCreateTime}")
    public TableDataInfo ownerShipStatisticsList(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime
            , @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime
            , Integer pageNum , Integer pageSize){

        log.info("开始执行销售统计归属人报表：{}，{}",beginCreateTime,endCreateTime,pageNum,pageSize);

        TableDataInfo tableDataInfo = reportService.ownerShipStatisticsList(beginCreateTime,endCreateTime,pageNum,pageSize);

        if (tableDataInfo != null){
            AjaxResult success = AjaxResult.success();
            BeanUtils.copyProperties(success,tableDataInfo);
        }else {
            AjaxResult success = AjaxResult.error();
            BeanUtils.copyProperties(success,tableDataInfo);
        }

        return tableDataInfo;
    }





}
